package com.hdcircles.autoform.ui.label.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cengalabs.flatui.views.FlatCheckBox;
import com.hdcircles.autoform.R;
import com.hdcircles.autoform.app.Constants;
import com.hdcircles.autoform.base.BaseActivity;
import com.hdcircles.autoform.model.bean.QuestionBean;
import com.hdcircles.autoform.presenter.MultipleChoicePresenter;
import com.hdcircles.autoform.presenter.contract.MultipleChoiceContract;
import com.hdcircles.autoform.util.SnackbarUtil;
import com.hdcircles.autoform.widget.ChoiceAnswerView;
import com.jaredrummler.materialspinner.MaterialSpinner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by hdcircles on 2016/9/29.
 */

public class MultipleChoiceActivity extends BaseActivity<MultipleChoicePresenter> implements MultipleChoiceContract.View, ChoiceAnswerView.onEditListener {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_question)
    EditText etQuestion;
    @BindView(R.id.ll_add_answer)
    LinearLayout llAddAnswer;
    @BindView(R.id.spinner_type)
    MaterialSpinner spinnerType;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_save)
    TextView tvSave;
    @BindView(R.id.view_answer)
    ChoiceAnswerView viewAnswer;
    @BindView(R.id.cb_required)
    CheckBox cbRequired;
    QuestionBean questionBean;

    private List<ChoiceAnswerView> answerViewList;

    private String[] types = new String[]{"单选", "多选"};

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_multiple_choice;
    }

    @Override
    protected void initEventAndData() {
        setToolBar(toolbar, "增加选择题");
        Intent intent = getIntent();
        questionBean = intent.getParcelableExtra("question");
        answerViewList = new ArrayList<>();
        answerViewList.add(viewAnswer);
        viewAnswer.getIvEdit().setBackgroundResource(R.drawable.ic_add_question);
        questionBean.setType(Constants.TYPE_SINGLE_CHOOSE);
        spinnerType.setItems(types);
        spinnerType.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
                switch (position) {
                    case 0:
                        questionBean.setType(Constants.TYPE_SINGLE_CHOOSE);
                        break;
                    case 1:
                        questionBean.setType(Constants.TYPE_MULTIPLE_CHOOSE);
                        break;
                }
            }
        });
        viewAnswer.setEditListener(this);
    }


    @Override
    public void onEdit(View view) {
        ChoiceAnswerView v = (ChoiceAnswerView) view;
        if (v.isLastItem()) {
            v.setLastItem(false);
            v.getIvEdit().setBackgroundResource(R.drawable.ic_delete);
//            v.getIvEdit().setBackground(getResources().getDrawable(R.drawable.ic_delete));
            ChoiceAnswerView answerView = new ChoiceAnswerView(this);
            answerView.setEditListener(this);
            answerView.getIvEdit().setBackgroundResource(R.drawable.ic_add_question);
            llAddAnswer.addView(answerView);
            answerViewList.add(answerView);
            return;
        }
        llAddAnswer.removeView(v);
        answerViewList.remove(v);
    }

    @OnClick(R.id.tv_cancel)
    void cancel() {
        onBackPressedSupport();
    }

    @OnClick(R.id.tv_save)
    void save() {
        if (etQuestion.getText().length() == 0) {
            etQuestion.setError("不能为空");
        }
        if (mPresenter.validate(answerViewList) && etQuestion.getText().length() != 0) {
            questionBean.setQuestionTitle(etQuestion.getText().toString().trim());
            questionBean.setRequired(cbRequired.isChecked());
            mPresenter.genQuestion(answerViewList, questionBean);
            return;
        }
        SnackbarUtil.showShort(toolbar, "请先完善填空");
    }

    @Override
    public void toSurvey(QuestionBean questionBean) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("question", questionBean);
        Intent intent = getIntent();
        intent.putExtras(bundle);
        setResult(Constants.TYPE_MULTIPLE_CHOOSE, intent);
        finish();
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) onBackPressedSupport();
        return super.onOptionsItemSelected(item);
    }

}
